﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FFS.Model;
using FFS.BLL;
using FFS.Common;

namespace FFS
{
    public partial class frmFinanceDetails : Form
    {
        public frmFinanceDetails()
        {
            InitializeComponent();
        }

        FinanceItemService financeItemService = new FinanceItemService();
        FamilyMemberService familyService = new FamilyMemberService();
        FinanceDetailService financeDetailService = new FinanceDetailService();

        List<FinanceItemInfo> financeItemList = new List<FinanceItemInfo>();
        List<FamilyMemberInfo> familyList = new List<FamilyMemberInfo>();
        List<FinanceDetailInfo> detailList = new List<FinanceDetailInfo>();

        FinanceDetailInfo financedetail = new FinanceDetailInfo();

        private string actionType = "";

        private void BindDataGridView(List<FinanceDetailInfo> detailList) 
        {
            if (detailList.Count > 0)
            {
                foreach (FinanceDetailInfo detail in detailList)
                {
                    this.dgv_FinanceDetails.Rows.Clear();
                    this.dgv_FinanceDetails.Rows.Add(detail.FinanceDetailID, detail.FamilyMemberID, detail.FamilyMemberName, detail.FinanceItemKey, detail.FinanceItemName, detail.FinanceTypeKey, detail.FinanceTypeName, detail.FinanceAmount, detail.Input_Date, detail.Input_User);
                }
            }
        }

        private void frmFinanceDetails_Load(object sender, EventArgs e)
        {
            this.cmb_FamilyMember.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmb_FinanceItem.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmb_FinanceType.DropDownStyle = ComboBoxStyle.DropDownList;

            ComboBoxOption option1 = new ComboBoxOption("IN","收入");
            ComboBoxOption option2 = new ComboBoxOption("OUT","支出");
            this.cmb_FinanceType.Items.Clear();
            this.cmb_FinanceType.Items.Add(option1);
            this.cmb_FinanceType.Items.Add(option2);
            this.cmb_FinanceType.DisplayMember = "Value";
            this.cmb_FinanceType.ValueMember = "Key";
            this.cmb_FinanceType.SelectedIndex = 0;

            financeItemList = financeItemService.ManageFinanceItem().GetAllFinanceItem();
            if (financeItemList.Count > 0)
            {
                this.cmb_FinanceItem.Items.Clear();
                foreach (FinanceItemInfo item in financeItemList)
                {
                    this.cmb_FinanceItem.Items.Add(new ComboBoxOption(item.financeItemKey,item.financeItemName));
                }
                this.cmb_FinanceItem.DisplayMember = "Value";
                this.cmb_FinanceItem.ValueMember = "Key";
                this.cmb_FinanceItem.SelectedIndex = 0;
            }

            familyList = familyService.ManageFamilyMember().GetAllFamilyMember();
            if (familyList.Count > 0)
            {
                this.cmb_FamilyMember.Items.Clear();
                foreach (FamilyMemberInfo family in familyList)
                {
                    this.cmb_FamilyMember.Items.Add(new ComboBoxOption(family.FamilyMemberID.ToString(),family.FamilyMemberName.ToString()));
                }
                this.cmb_FamilyMember.DisplayMember = "Value";
                this.cmb_FamilyMember.ValueMember = "Key";
                this.cmb_FamilyMember.SelectedIndex = 0;
            }

            this.cmb_searchItem.Items.Clear();
            this.cmb_searchItem.Items.Add(new ComboBoxOption("FamilyMemberName", "家庭成员名称"));
            this.cmb_searchItem.Items.Add(new ComboBoxOption("FinanceItemName", "财务项目名称"));
            this.cmb_searchItem.Items.Add(new ComboBoxOption("FinanceTypeName", "财务类型"));
            this.cmb_searchItem.Items.Add(new ComboBoxOption("FinanceAmount", "财务金额"));
            this.cmb_searchItem.Items.Add(new ComboBoxOption("FinanceRemark", "财务备注"));
            this.cmb_searchItem.DisplayMember = "Value";
            this.cmb_searchItem.ValueMember = "Key";
            this.cmb_searchItem.Items.Add(new ComboBoxOption("input_user", "录入用户"));
            this.cmb_searchItem.SelectedIndex = 0;
            this.panel_EditFinanceDetails.Visible = false;
            this.panel_search.Visible = false;
            
            //初始化datagridView
            this.dgv_FinanceDetails.Columns.Add("FinanceDetailID","财务明细编号");
            this.dgv_FinanceDetails.Columns[0].Visible = false;
            this.dgv_FinanceDetails.Columns.Add("FamilyMemberID", "家庭成员编号");
            this.dgv_FinanceDetails.Columns[1].Visible = false;
            this.dgv_FinanceDetails.Columns.Add("FamilyMemberName", "家庭成员名称");
            this.dgv_FinanceDetails.Columns.Add("FinanceItemKey", "财务项目主键");
            this.dgv_FinanceDetails.Columns[3].Visible = false;
            this.dgv_FinanceDetails.Columns.Add("FinanceItemName", "财务项目名称");
            this.dgv_FinanceDetails.Columns.Add("FinanceTypeKey", "财务类型主键");
            this.dgv_FinanceDetails.Columns[5].Visible = false;
            this.dgv_FinanceDetails.Columns.Add("FinanceTypeName", "财务类型名称");
            this.dgv_FinanceDetails.Columns.Add("FinanceAmount", "财务金额");
            this.dgv_FinanceDetails.Columns.Add("FinanceRemark", "财务备注");
            this.dgv_FinanceDetails.Columns.Add("input_date", "录入时间");
            this.dgv_FinanceDetails.Columns.Add("input_user", "录入用户");

            this.BindDataGridView(financeDetailService.ManageFinanceDetail().GetAllFinanceDetails());
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            ComboBoxOption selectedFamily = (ComboBoxOption)this.cmb_FamilyMember.SelectedItem;
            ComboBoxOption selectedFinanceItem = (ComboBoxOption)this.cmb_FinanceItem.SelectedItem;
            ComboBoxOption selectedFinanceType = (ComboBoxOption)this.cmb_FinanceType.SelectedItem;

            if (this.nud_FinanceAmount.Value == 0 || selectedFamily.Key == "" || selectedFinanceItem.Key == "" || selectedFinanceType.Key == "")
            {
                MessageBox.Show("录入金额|家庭成员|财务项目|财务类型都不能为空！");
            }
            else
            {
                financedetail.FinanceDetailID = int.Parse(this.nud_FinanceDetailID.Value.ToString());
                financedetail.FamilyMemberID = int.Parse(selectedFamily.Key);
                financedetail.FinanceAmount = this.nud_FinanceAmount.Value;
                financedetail.FinanceItemKey = selectedFinanceItem.Key;
                financedetail.FinanceTypeKey = selectedFinanceType.Key;
                financedetail.FinanceRemark = this.txt_FinanceRemark.Text;
                financedetail.Input_User = Context._Login_UserName;

                if (actionType == "Add")
                {
                    if (financeDetailService.ManageFinanceDetail().CreateFinanceDetail(financedetail) == 1)
                    {
                        MessageBox.Show("添加成功");
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                    }
                }
                else if (actionType == "Edit")
                {
                    if (financeDetailService.ManageFinanceDetail().ModifyFinanceDetail(financedetail) == 1)
                    {
                        MessageBox.Show("编辑成功");
                    }
                    else
                    {
                        MessageBox.Show("编辑失败");
                    }
                }
                else if (actionType == "Delete")
                {
                    if (financeDetailService.ManageFinanceDetail().DeteleFinanceDetail(financedetail) == 1)
                    {
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }

            this.BindDataGridView(financeDetailService.ManageFinanceDetail().GetAllFinanceDetails());

        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            string searchContent = "  1=1 ";
            ComboBoxOption selectedOption =  (ComboBoxOption)this.cmb_searchItem.SelectedItem;
            string selectedItem = selectedOption.Key;
            if (selectedItem.Trim() == "FinanceAmount")
            {
                searchContent += " " + selectedItem + " = " + this.nud_FinanceAmount.ToString();
            }
            else 
            {
                searchContent += " " + selectedItem + " like N'%" + this.txt_searchContent.ToString() + "%'";
            }
        }

        private void cmb_searchItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBoxOption selectedOption = (ComboBoxOption)this.cmb_searchItem.SelectedItem;
            if (selectedOption.Key == "FinanceAmount")
            {
                this.txt_searchContent.Visible = false;
                //this.nud_FinanceAmount.Location.X = 297;
                //this.nud_FinanceAmount.Location.Y = 13;
                this.nud_searchAmount.Height = 21;
                this.nud_searchAmount.Width = 210;
                this.nud_searchAmount.Visible = true;
            }
            else 
            {
                this.nud_searchAmount.Visible = false;
                //this.txt_searchContent.Location.X = 297;
                //this.txt_searchContent.Location.Y = 13;
                this.txt_searchContent.Height = 21;
                this.txt_searchContent.Width = 210;
                this.txt_searchContent.Visible  = true;
            }
        }

        private void btn_CancelSearch_Click(object sender, EventArgs e)
        {
            this.panel_search.Visible = false;
            this.cmb_searchItem.SelectedIndex = 0;
            this.txt_searchContent.Text = "";
            this.nud_FinanceAmount.Value = 0;
            this.tsb_search.Enabled = true;
            this.tsb_add.Enabled = true;
            this.tsb_delete.Enabled = true;
            this.tsb_edit.Enabled = true;
            this.tsb_export.Enabled = true;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.tsb_add.Enabled = true;
            this.tsb_delete.Enabled = true;
            this.tsb_edit.Enabled = true;
            this.tsb_export.Enabled = true;
            this.tsb_search.Enabled = true;
            this.panel_EditFinanceDetails.Visible = false;
        }

        private void ControlEnabled(bool IsEnabled)
        {
            this.cmb_FamilyMember.Enabled = IsEnabled;
            this.cmb_FinanceItem.Enabled = IsEnabled;
            this.cmb_FinanceType.Enabled = IsEnabled;
            this.nud_FinanceAmount.Enabled = IsEnabled;
            this.txt_FinanceRemark.Enabled = IsEnabled;
        }

        private void tsb_add_Click(object sender, EventArgs e)
        {
            this.actionType = "Add";
            this.ControlEnabled(true);
            this.tsb_delete.Enabled = false;
            this.tsb_edit.Enabled = false;
            this.tsb_export.Enabled = false;
            this.tsb_search.Enabled = false;
            this.panel_EditFinanceDetails.Visible = true;
        }

        private void tsb_edit_Click(object sender, EventArgs e)
        {
            this.actionType = "Edit";
            this.ControlEnabled(true);
            this.tsb_delete.Enabled = false;
            this.tsb_add.Enabled = false;
            this.tsb_export.Enabled = false;
            this.tsb_search.Enabled = false;
            this.panel_EditFinanceDetails.Visible = true;
        }

        private void tsb_delete_Click(object sender, EventArgs e)
        {
            this.actionType = "Delete";
            this.ControlEnabled(false);
            this.tsb_edit.Enabled = false;
            this.tsb_add.Enabled = false;
            this.tsb_export.Enabled = false;
            this.tsb_search.Enabled = false;
            this.panel_EditFinanceDetails.Visible = true;
        }

        private void tsb_search_Click(object sender, EventArgs e)
        {
            this.tsb_add.Enabled = false;
            this.tsb_edit.Enabled = false;
            this.tsb_export.Enabled = false;
            this.tsb_delete.Enabled = false;
            this.panel_search.Visible = true;
        }

        private void tsb_export_Click(object sender, EventArgs e)
        {

        }

        private void dgv_FinanceDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.cmb_FamilyMember.SelectedValue = this.dgv_FinanceDetails.SelectedRows[0].Cells[1].Value;
                this.cmb_FinanceItem.SelectedValue = this.dgv_FinanceDetails.SelectedRows[0].Cells[3].Value;
                this.cmb_FinanceType.SelectedValue = this.dgv_FinanceDetails.SelectedRows[0].Cells[5].Value;
                this.nud_FinanceAmount.Value = decimal.Parse(this.dgv_FinanceDetails.SelectedRows[0].Cells[7].Value.ToString());
                this.txt_FinanceRemark.Text = this.dgv_FinanceDetails.SelectedRows[0].Cells[8].Value.ToString();
            }
            catch (Exception)
            {

            }
        }
    }
}
